package com.powernode.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class AppListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("项目启动，ServletContext被创建");
        //servletContext被创建
        ServletContext context = servletContextEvent.getServletContext();
        //向context中放项目部署的名字
        String contextPath = context.getContextPath();
        System.out.println("项目部署名:"+contextPath+" 被放到ServletContext作用域，可以在jsp页面使用${ctx}获取");
        context.setAttribute("ctx",contextPath);
    }

    /**
     * tomcat停止时调用
     * @param servletContextEvent
     */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("tomcat被关闭了");
    }
}
